package com.cghs.ac.trafficCalc;

import java.util.Calendar;

import android.database.Cursor;
import android.net.TrafficStats;
import android.os.Process;

public class TrafficHelper {
	//数据库中的 wifi 流量
	private long mOldWifiTraffic;
	//数据库中的 移动 流量
	private long mOldMobileTraffic;
	
	private long mLastRx;
	private long mLastTx;
	private long mNewWifiTraffic;
	private long mNewMobileTraffic;
	private DBService mDbService;
	private int mMonth;
	
	//流量统计工具类
	public TrafficHelper(DBService db) {
		mDbService = db;
		Calendar calendar = Calendar.getInstance();
		mMonth = calendar.get(Calendar.MONTH) + 1;
		getTrafficstatFromDB();
	}
	
	public void getTrafficstatFromDB() {
		String sql = "select * from traffic where month="+mMonth;
		Cursor cursor = mDbService.query(sql, null);
		if (cursor != null && cursor.getCount() != 0) {
			cursor.moveToFirst();
			setOldWifiTraffic(cursor.getLong(cursor.getColumnIndex("wifi")));
			setOldMobileTraffic(cursor.getLong(cursor.getColumnIndex("mobile")));
			setLastRx(cursor.getLong(cursor.getColumnIndex("lastrx")));
			setLastTx(cursor.getLong(cursor.getColumnIndex("lasttx")));
			cursor.close();
		} else {
			//如果是新的一月 则获取上个月 lasttx 和lastrx
			String sql2 = "select * from traffic where month="+(mMonth-1);
			Cursor cursor2 = mDbService.query(sql2, null);
			if (cursor2.getCount() != 0) {
			    cursor2.moveToFirst();
				setLastRx(cursor2.getLong(cursor2.getColumnIndex("lastrx")));
				setLastTx(cursor2.getLong(cursor2.getColumnIndex("lasttx")));
			}
			cursor2.close();
			
			//初始化新一个月的记录
			String addSql = "insert into traffic(_id, month, wifi, mobile, lasttx, lastrx) values(null,?,?,?,?,?)";
			Object[] args = new Object[] {mMonth, 0, 0, mLastTx, mLastRx};
			mOldWifiTraffic = 0;
			mOldMobileTraffic = 0;
			mDbService.execSQL(addSql, args);
		}
	}
	
	public void saveTrafficstatToDB() {
		String sql = "update traffic set mobile=?, wifi=?, lasttx=?, lastrx=? where month= ?";
		Object[] args = new Object[] {mNewMobileTraffic , mNewWifiTraffic, mLastTx, mLastRx, mMonth};
		mDbService.execSQL(sql, args);
	}
	
	public void saveTrafficstatToDB(long mobile , long wifi) {
		String sql = "update traffic set mobile=?, wifi=? where month= ?";
		Object[] args = new Object[] {mobile , wifi, mMonth};
		mDbService.execSQL(sql, args);
	}
	
	public static long getAdditionTraffic() {
		return TrafficStats.getUidRxBytes(Process.myUid()) + TrafficStats
				.getUidTxBytes(Process.myUid());
	}
	
	public long getOldWifiTraffic() {
		return mOldWifiTraffic;
	}

	public void setOldWifiTraffic(long OldWifiTraffic) {
		this.mOldWifiTraffic = OldWifiTraffic;
	}

	public long getOldMobileTraffic() {
		return mOldMobileTraffic;
	}

	public void setOldMobileTraffic(long OldMobileTraffic) {
		this.mOldMobileTraffic = OldMobileTraffic;
	}

	public long getNewWifiTraffic() {
		return mNewWifiTraffic;
	}

	public void setNewWifiTraffic(long NewWifiTraffic) {
		this.mNewWifiTraffic = NewWifiTraffic;
	}

	public long getNewMobileTraffic() {
		return mNewMobileTraffic;
	}

	public void setNewMobileTraffic(long NewMobileTraffic) {
		this.mNewMobileTraffic = NewMobileTraffic;
	}

	public long getLastTx() {
		return mLastTx;
	}

	public void setLastTx(long LastTx) {
		this.mLastTx = LastTx;
	}

	public long getLastRx() {
		return mLastRx;
	}

	public void setLastRx(long LastRx) {
		this.mLastRx = LastRx;
	}
}
